package com.danicsoft.daide.command;

import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap.Commands;

/**
 * Implements the HUH command. From Eric Wald's <a
 * href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>,
 * v0.14:
 * <h4><a name="HUH">server_message = HUH</a> (message)</h4>
 * <p>
 * This message is sent from the server to the client, and means that the server
 * determined that <b>message</b> had a syntax error in it. The token <b>ERR</b>
 * is inserted into the message immediately before the first offending token.
 * </p>
 * <p>
 * This error can also be caused by trying to use a form of the Diplomacy AI
 * Development Environment syntax which is not available at the syntax level for
 * the current game.
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class HuhCommand extends Command {

	/**
	 * The group of tokens that has an error in it. This group must already have
	 * an err token inserted into it.
	 */
	private TokenGroup errorTokens;

	/**
	 * The message that describes the error encountered.
	 */
	private String errorMessage = "Recieved error message.";

	/**
	 * Construct a new HuhCommand from the tokens given.
	 * 
	 * @param tokens
	 *        The set of tokens that have an error in them. This group must
	 *        already have an ERR token in it.
	 */
	public HuhCommand(TokenGroup tokens) {
		errorTokens = tokens;
	}

	/**
	 * Construct a new HuhCommand from the tokens and message given.
	 * 
	 * @param tokens
	 *        A TokenGroup with an error in it. It must not yet have an ERR
	 *        token in it.
	 * @param errorMessage
	 *        The message associated with this error.
	 */
	public HuhCommand(TokenGroup tokens, String errorMessage) {
		tokens.insertErr();
		errorTokens = tokens;
		this.errorMessage = errorMessage;
	}

	/**
	 * Construct a new HuhCommand from the tokens in a given command and a given
	 * message.
	 * 
	 * @param errCommand
	 *        The Command that has a problem with it.
	 * @param errorMessage
	 *        The reason for the error.
	 */
	public HuhCommand(Command errCommand, String errorMessage) {
		errorTokens = errCommand.output();
		errorTokens.insertErr();
		this.errorMessage = errorMessage;
	}

	@Override
	public byte getType() {
		return Commands.HUH;
	}

	@Override
	public String toString() {
		return "Error: " + errorMessage;
	}

	/**
	 * Does nothing. Should never be called.
	 * 
	 * @param tokenIO
	 *        Ignored
	 * @param tokens
	 *        Ignored
	 * @throws ParseException
	 *         Never.
	 * @see com.danicsoft.daide.command.IOTarget#doIO(com.danicsoft.daide.command.TokenIO,
	 *      com.danicsoft.daide.token.TokenGroup)
	 */
	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
	// Intentionally left blank

	}

	/**
	 * Outputs this HuhMessage. Because this command only needs to output the
	 * TokenGroup that it already has stored, it cannot use the default
	 * implementation of {@link Command#output()}.
	 * 
	 * @return The TokenGroup that contains an error.
	 * @see com.danicsoft.daide.command.Command#output()
	 */
	@Override
	public TokenGroup output() {
		return errorTokens;
	}

	/**
	 * Get the error message.
	 * 
	 * @return The error message.
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

}
